// populate scripts in a scriptsListBox
function helper_populateScripts(scriptsListBox, stringBundle) {
	const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";

	var selectedIndex = scriptsListBox.selectedIndex;

	while(scriptsListBox.hasChildNodes())
		scriptsListBox.removeChild(scriptsListBox.firstChild);

	var scripts = TH_getInfo('scripts');

	for (var i = 0; i < scripts.length; i++) {
		var richListItem = document.createElementNS(XUL_NS, 'richlistitem');
		richListItem.setAttribute('class', 'scriptItem');
		scriptsListBox.appendChild(richListItem);

		var box1 = document.createElementNS(XUL_NS, 'vbox');
		richListItem.appendChild(box1);

		var box11 = document.createElementNS(XUL_NS, 'hbox');
		box1.appendChild(box11);

		var label = document.createElementNS(XUL_NS, 'label');
		label.setAttribute('class', 'scriptName');
		label.setAttribute('value', scripts[i].name);
		box11.appendChild(label);

		var label = document.createElementNS(XUL_NS, 'label');
		label.setAttribute('class', 'scriptVersion');
		label.setAttribute('value', scripts[i].version);
		box11.appendChild(label);

		var description = document.createElementNS(XUL_NS, 'description');
		description.setAttribute('class', 'scriptDescription');
		description.setAttribute('value', scripts[i].description);
		box1.appendChild(description);

		var box12 = document.createElementNS(XUL_NS, 'hbox');
		box12.setAttribute('class', 'scriptActionBox');
		box1.appendChild(box12);

		var button = document.createElementNS(XUL_NS, 'button');

		if (scripts[i].active == 'false') {
			button.setAttribute('disabled', 'true');
			button.setAttribute('label', stringBundle.getString('scriptsManager_inactiveButton_label'));
		} else if (scripts[i].enabled == 'true')
			button.setAttribute('label', stringBundle.getString('scriptsManager_disableButton_label'));
		else
			button.setAttribute('label', stringBundle.getString('scriptsManager_enableButton_label'));

		box12.appendChild(button);

		button.onclick = function () {
			if (this.disabled)
				return;

			var index = this.parentNode.parentNode.parentNode.parentNode.selectedIndex;
			var store = TH_getDataStore('system');
			var scripts = TH_getInfo('scripts');

			if (scripts[index].enabled == 'true') {
				this.setAttribute('label', stringBundle.getString('scriptsManager_enableButton_label'));

				store.update(
					{subject: scripts[index].url, predicate: 'isEnabled', object: 'true'},
					{object: 'false'});
			} else {
				this.setAttribute('label', stringBundle.getString('scriptsManager_disableButton_label'));

				store.update(
					{subject: scripts[index].url, predicate: 'isEnabled', object: 'false'},
					{object: 'true'});
			}

			store.save();
		};

		var button = document.createElementNS(XUL_NS, 'button');
		button.setAttribute('label', stringBundle.getString('scriptsManager_executeButton_label'));
		box12.appendChild(button);

		button.onclick = function () {
			var index = this.parentNode.parentNode.parentNode.parentNode.selectedIndex;

			var scripts = TH_getInfo('scripts');

			try {
				TH_injectScriptDefault(scripts[index]);
			} catch (e) {
				if (e == 'scheme not supported')
					alert(stringBundle.getString('schemeNotSupported'));
			}
		};
	}

	scriptsListBox.selectedIndex = selectedIndex;
}